<template>
  <el-form ref="form" :model="form" label-width="100px">
    <el-form-item v-for="(item, index) in fields" :key="index" :label="item.name" :prop="item.field">
      <el-input v-model="form[item.field]" @change="onChange" type="text" placeholder="请输入"></el-input>
    </el-form-item>
  </el-form>
</template>

<script>
  export default {
    props: {
      value: {
        type: String,
        default: null
      }
    },
    watch: {
      value: {
        handler(val) {
          try {
            this.form = val ? JSON.parse(val) : {};
          } catch (e) {
            this.form = {}
          }
        },
        immediate: true
      }
    },
    data() {
      return {
        form: {},
        fields: []
      };
    },
    created() {
      try {
        this.fields = this.$store.getters.tenantInfo?.profile ? JSON.parse(this.$store.getters.tenantInfo.profile) : []
      } catch (e) {
        this.fields = []
      }
    },
    methods: {
      onChange() {
        const str = JSON.stringify(this.form)
        this.$emit('input', str);
      },
    }
  };
</script>
